<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Index extends Base
{
    // 系统设置
    public function systemSet()
    {
        $res=Db::name('system_set')->field('protocol,commission_scale')->where('id',1)->find();
        if($res){
            $res['protocol'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['protocol']);
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
  	//轮播图
	public function banner(){
        // $list=Db::name('banner')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.*,b.img')->where('a.state',1)->order('a.sort desc')->select();
        $list=Db::name('banner')->where('state',1)->order('sort desc')->select();
        if($list){
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$list]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
	}
    //公告
    public function announcement(){
        $list=Db::name('announcement')->where('state',1)->field('id,title')->order('sort desc')->select();
        if($list){
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$list]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
    //公告详情
    public function announcementDetail(){
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 501, 'msg' => 'id']);
        }
        $res=Db::name('announcement')->where('id',$id)->find();
        if($res){
            $res['detail'] = str_replace('/ueditor/php/', $this->request->domain() . '/ueditor/php/', $res['detail']);
            $res['uptime']=date('Y-m-d H:i:s',$res['uptime']);
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }

    //农副产品
    public function product(){
        $products=Db::name('product')->where('state',1)->field('id,title,img')->order('id desc')->limit(0,4)->select();
        if($products){
            foreach ($products as $key => $value) {
                $products[$key]['price']=Db::name('spec_combination')->where('p_id',$products[$key]['id'])->where('state',1)->order('id asc')->value('money');
            }
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$products]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
    //产品详情
    public function productDetail(){
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 501, 'msg' => '详情id呢？']);
        }
        $res=Db::name('product')->where('id',$id)->find();
        if($res){
            $res['detail'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['detail']);
            $res['imgs']=explode(',',$res['imgs']);
            $res['price']=Db::name('spec_combination')->where('p_id',$id)->where('state',1)->order('id asc')->value('money');
            $res['spec']=Db::name('spec')->where('p_id',$id)->where('state',1)->order('id asc')->select();
            if ($res['spec']) {
                foreach ($res['spec'] as $key => $value) {
                    $res['spec'][$key]['spec_vals']=Db::name('spec_val')->where('p_id',$id)->where('spec_id',$res['spec'][$key]['id'])->where('state',1)->order('id asc')->select();
                }
            }
            $res['spec_combination']=Db::name('spec_combination')->where('p_id',$id)->where('state',1)->order('id asc')->select();
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
    // 一级分类
    public function cates()
    {
        $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
        if($cates){
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$cates]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
    public function spots()
    {
        if(!empty(input("param.latitude"))){
            $latitude=input("param.latitude");
        }else{
            return json(['code' => 501, 'msg' => '纬度呢']);
        }
        if(!empty(input("param.longitude"))){
            $longitude=input("param.longitude");
        }else{
            return json(['code' => 502, 'msg' => '经度呢']);
        }
        $pagef=4*(1-1);
        $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 order by id desc) as a where a.distance<=5000000 limit ".$pagef.",4";
        $res=Db::query($sql);
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
        
    }
    //门票详情
    public function spotDetail(){
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 501, 'msg' => '详情id呢？']);
        }
        $res=Db::name('scenic_spot')->where('id',$id)->find();
        if($res){
            $res['instructions'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['instructions']);
            $res['scenicspot_instructions'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['scenicspot_instructions']);
            $res['tourism_strategy'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['tourism_strategy']);
            $res['detail'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['detail']);
            $res['imgs']=explode(',',$res['imgs']);
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
}